<!DOCTYPE html><html lang="zh-tw"><head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Set&#x548C;Map&#x6570;&#x636E;&#x7ED3;&#x6784; | ECMAScript 6&#x5165;&#x95E8;</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.4.3">
        <meta name="author" content="&#x5927;&#x9F3B;&#x5B50;">
        
        <meta name="HandheldFriendly" content="true">
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="https://wohugb.gitbooks.io/ecmascript-6/content/gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="https://gstatic.gitbook.com/images/b1c4c86ca817a71628d2debe73be9a64.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="static/css/style.css">
    
        
        <link rel="stylesheet" href="static/css/website.css">
        
    
    

        
    
    
    <link rel="next" href="iterator.html">
    
    
    <link rel="prev" href="function.html">
    

        
    
<style>
    .book .book-summary .book-search,
    .book .book-summary ul.summary {
        position: relative;
        top: auto;
        left: auto;
        right: auto;
        bottom: auto;
    }
    .book-summary {
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        flex-direction: column;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        overflow-y: auto;
    }
    .book-summary > nav {
        flex: 1 1 auto;
        overflow-y: auto;
        overflow-y: overlay;
    }
    .gl-wrapper {
        margin: 0;
        display: block;
        padding: 0;
        position: relative;
        line-height: 15px;
        background: #fafafa;
    }
    .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
        background: -moz-linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
        top: -24px;
        width: 100%;
        height: 24px;
        content: "";
        display: block;
        position: absolute;
        background: linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
    }
    .book.color-theme-1 .gl-wrapper {
        background: #121212;
    }
    .book.color-theme-1 .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
        background: -moz-linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
        background: linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
    }
    .book.color-theme-2 .gl-wrapper {
        background: #2e3243;
    }
    .book.color-theme-2 .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
        background: -moz-linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
        background: linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
    }
    .gl-wrapper .gitbook-link {
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        color: #9DAAB6;
        margin: 16px;
        display: flex;
        padding: 0;
        align-items: center;
        margin-left: 32px;
        padding-top: 8px;
        padding-left: 16px;
        border-radius: 3px;
        padding-right: 16px;
        padding-bottom: 8px;
        text-decoration: none;
        background-color: #E6ECF1;
        -webkit-box-align: center;
    }
    .gl-wrapper .tm-logo {
        margin: 0;
        display: block;
        padding: 0;
        font-size: 40px;
    }
    .gl-wrapper .tm-content {
        flex: 1;
        margin: 0;
        display: block;
        padding: 0;
        padding-left: 16px;
    }
    .gl-wrapper .tm-content .container {
        margin: 0;
        display: block;
        padding: 0;
    }
    .gl-wrapper .tm-content .tm-container .tm-text {
        font-size: 12px;
        font-family: "Roboto", sans-serif;
        font-weight: 400;
        line-height: 1.625;
    }
</style></head>
    <body>
        
        
    <div class="book" data-level="9" data-basepath=".." data-revision="Sun Oct 25 2015 02:52:03 GMT+0000 (UTC)">
    

<div class="book-summary">
    <div class="book-search" role="search">
        <input type="text" placeholder="&#x8F38;&#x5165;&#x4E26;&#x641C;&#x5C0B;" class="form-control">
    </div>
    <nav role="navigation">
        <ul class="summary">
            
            
            
                
                <li>
                    <a href="javascript:;" target="blank" class="custom-link">ECMAScript 6&#x5165;&#x95E8;</a>
                </li>
            
            

            
            <li class="divider"></li>
            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="index.html">
                
                        <i class="fa fa-check"></i>
                        
                        &#x524D;&#x8A00;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/intro.html">
            
                
                    <a href="intro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        &#x7B80;&#x4ECB;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/let.html">
            
                
                    <a href="let.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        let&#x548C;const&#x547D;&#x4EE4;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/destructuring.html">
            
                
                    <a href="destructuring.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        &#x53D8;&#x91CF;&#x7684;&#x89E3;&#x6784;&#x8D4B;&#x503C;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/string.html">
            
                
                    <a href="string.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        &#x5B57;&#x7B26;&#x4E32;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/number.html">
            
                
                    <a href="number.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        &#x6570;&#x503C;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/array.html">
            
                
                    <a href="array.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        &#x6570;&#x7EC4;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/object.html">
            
                
                    <a href="object.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        &#x5BF9;&#x8C61;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/function.html">
            
                
                    <a href="function.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        &#x51FD;&#x6570;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="9" data-path="docs/set-map.html">
            
                
                    <a href="">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        Set&#x548C;Map&#x6570;&#x636E;&#x7ED3;&#x6784;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10" data-path="docs/iterator.html">
            
                
                    <a href="iterator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Iterator&#x548C;for...of&#x5FAA;&#x73AF;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11" data-path="docs/generator.html">
            
                
                    <a href="generator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        Generator &#x51FD;&#x6570;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="docs/promise.html">
            
                
                    <a href="promise.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        Promise&#x5BF9;&#x8C61;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="13" data-path="docs/class.html">
            
                
                    <a href="class.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                        Class&#x548C;Module
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14" data-path="docs/reference.html">
            
                
                    <a href="reference.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        &#x53C2;&#x8003;&#x94FE;&#x63A5;
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            
            
        </ul>
    </nav>

<div class="gl-wrapper">
    <a class="gitbook-link" href="javascript:;" target="blank">
        <div class="tm-logo">
            <svg preserveaspectratio="xMidYMid meet" height="1em" width="1em" fill="currentColor" viewbox="0 0 1067 769" xmlns="http://www.w3.org/2000/svg" stroke="none" class="icon-7f6730be--text-3f89f380"><g><path d="M480.026 640.677c17.205 0 31.2 13.997 31.2 31.194s-13.995 31.193-31.2 31.193c-17.197 0-31.193-13.996-31.193-31.193 0-17.197 13.996-31.194 31.193-31.194m489.93-193.226c-17.203 0-31.2-13.998-31.2-31.195 0-17.204 13.997-31.2 31.2-31.2 17.198 0 31.194 13.996 31.194 31.2 0 17.197-13.996 31.195-31.193 31.195m0-127.804c-53.269 0-96.609 43.34-96.609 96.609 0 10.373 1.723 20.702 5.123 30.741L559.328 616.879c-18.132-26.128-47.521-41.617-79.302-41.617-36.821 0-70.391 21.065-86.63 54.003L106.68 478.109c-30.288-15.927-52.965-65.817-50.56-111.223 1.248-23.687 9.438-42.071 21.897-49.17 7.916-4.493 17.436-4.099 27.526 1.188l1.916 1.01c75.96 40.022 324.6 170.981 335.063 175.844 16.157 7.47 25.14 10.5 52.659-2.547l513.958-267.3c7.53-2.844 16.315-10.062 16.315-21.023 0-15.205-15.72-21.199-15.765-21.199-29.218-14.018-74.163-35.054-117.987-55.57C798.033 84.26 691.861 34.547 645.23 10.132c-40.253-21.072-72.655-3.311-78.432.282l-11.227 5.555C345.727 119.743 64.898 258.826 48.911 268.553 20.278 285.973 2.547 320.679.252 363.768c-3.586 68.304 31.261 139.506 81.069 165.634l303.172 156.354c6.83 47.306 47.55 82.725 95.532 82.725 52.78 0 95.808-42.546 96.603-95.14L910.541 492.38c16.93 13.233 37.92 20.486 59.416 20.486 53.268 0 96.61-43.341 96.61-96.61s-43.342-96.61-96.61-96.61" fill-rule="evenodd"></path></g></svg>
        </div>
        <div class="tm-content">
            <div class="tm-container">
                <span class="tm-text">
                    Powered by <b>GitBook</b>
                </span>
            </div>
        </div>
    </a>
</div></div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="&#x76EE;&#x9304;"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="&#x641C;&#x5C0B;"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="&#x5B57;&#x578B;&#x8A2D;&#x5B9A;"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">&#x896F;&#x7DDA;&#x9AD4;</button>
        <button type="button" data-font="1" class="button">&#x7121;&#x896F;&#x7DDA;&#x9AD4;</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">&#x767D;&#x8272;</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">&#x68D5;&#x8910;&#x8272;</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">&#x591C;&#x9593;</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="&#x5206;&#x4EAB;"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    &#x5206;&#x4EAB;&#x5230; Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    &#x5206;&#x4EAB;&#x5230; Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    &#x5206;&#x4EAB;&#x5230; Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    &#x5206;&#x4EAB;&#x5230; Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    &#x5206;&#x4EAB;&#x5230; Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="index.html">ECMAScript 6&#x5165;&#x95E8;</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="set%E5%92%8Cmap%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84">Set&#x548C;Map&#x6570;&#x636E;&#x7ED3;&#x6784;</h1>
<h2 id="set">Set</h2>
<h3 id="%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95">&#x57FA;&#x672C;&#x7528;&#x6CD5;</h3>
<p>ES6&#x63D0;&#x4F9B;&#x4E86;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;Set&#x3002;&#x5B83;&#x7C7B;&#x4F3C;&#x4E8E;&#x6570;&#x7EC4;&#xFF0C;&#x4F46;&#x662F;&#x6210;&#x5458;&#x7684;&#x503C;&#x90FD;&#x662F;&#x552F;&#x4E00;&#x7684;&#xFF0C;&#x6CA1;&#x6709;&#x91CD;&#x590D;&#x7684;&#x503C;&#x3002;</p>
<p>Set&#x672C;&#x8EAB;&#x662F;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x751F;&#x6210;Set&#x6570;&#x636E;&#x7ED3;&#x6784;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> s = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>();

[<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">5</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>].map(x =&gt; s.add(x))

<span class="hljs-keyword">for</span> (i <span class="hljs-keyword">of</span> s) {<span class="hljs-built_in">console</span>.log(i)}
<span class="hljs-comment">// 2 3 4 5</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x901A;&#x8FC7;add&#x65B9;&#x6CD5;&#x5411;Set&#x7ED3;&#x6784;&#x52A0;&#x5165;&#x6210;&#x5458;&#xFF0C;&#x7ED3;&#x679C;&#x8868;&#x660E;Set&#x7ED3;&#x6784;&#x4E0D;&#x4F1A;&#x6DFB;&#x52A0;&#x91CD;&#x590D;&#x7684;&#x503C;&#x3002;</p>
<p>Set&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x521D;&#x59CB;&#x5316;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> items = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">5</span>,<span class="hljs-number">5</span>,<span class="hljs-number">5</span>]);

items.size 
<span class="hljs-comment">// 5</span>
</code></pre>
<p>&#x5411;Set&#x52A0;&#x5165;&#x503C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E0D;&#x4F1A;&#x53D1;&#x751F;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#xFF0C;5&#x548C;&#x201C;5&#x201D;&#x662F;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x503C;&#x3002;Set&#x5185;&#x90E8;&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x503C;&#x662F;&#x5426;&#x7CBE;&#x786E;&#x76F8;&#x7B49;&#xFF0C;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x7CBE;&#x786E;&#x76F8;&#x7B49;&#x8FD0;&#x7B97;&#x7B26;&#xFF08;===&#xFF09;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x603B;&#x662F;&#x4E0D;&#x76F8;&#x7B49;&#x7684;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>();

set.add({})
set.size <span class="hljs-comment">// 1</span>

set.add({})
set.size <span class="hljs-comment">// 2</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x8868;&#x793A;&#xFF0C;&#x7531;&#x4E8E;&#x4E24;&#x4E2A;&#x7A7A;&#x5BF9;&#x8C61;&#x4E0D;&#x662F;&#x7CBE;&#x786E;&#x76F8;&#x7B49;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x4EEC;&#x88AB;&#x89C6;&#x4E3A;&#x4E24;&#x4E2A;&#x503C;&#x3002;</p>
<h3 id="%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95">&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</h3>
<p>Set&#x7ED3;&#x6784;&#x6709;&#x4EE5;&#x4E0B;&#x5C5E;&#x6027;&#x3002;</p>
<ul>
<li>Set.prototype.constructor&#xFF1A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x9ED8;&#x8BA4;&#x5C31;&#x662F;Set&#x51FD;&#x6570;&#x3002;</li>
<li>Set.prototype.size&#xFF1A;&#x8FD4;&#x56DE;Set&#x7684;&#x6210;&#x5458;&#x603B;&#x6570;&#x3002;</li>
</ul>
<p>Set&#x6570;&#x636E;&#x7ED3;&#x6784;&#x6709;&#x4EE5;&#x4E0B;&#x65B9;&#x6CD5;&#x3002;</p>
<ul>
<li>add(value)&#xFF1A;&#x6DFB;&#x52A0;&#x67D0;&#x4E2A;&#x503C;&#xFF0C;&#x8FD4;&#x56DE;Set&#x7ED3;&#x6784;&#x672C;&#x8EAB;&#x3002;</li>
<li>delete(value)&#xFF1A;&#x5220;&#x9664;&#x67D0;&#x4E2A;&#x503C;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x5220;&#x9664;&#x662F;&#x5426;&#x6210;&#x529F;&#x3002;</li>
<li>has(value)&#xFF1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x8BE5;&#x503C;&#x662F;&#x5426;&#x4E3A;Set&#x7684;&#x6210;&#x5458;&#x3002;</li>
<li>clear()&#xFF1A;&#x6E05;&#x9664;&#x6240;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x3002;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x662F;&#x8FD9;&#x4E9B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x7684;&#x4F7F;&#x7528;&#x6F14;&#x793A;&#x3002;</p>
<pre><code class="lang-javascript">
s.add(<span class="hljs-number">1</span>).add(<span class="hljs-number">2</span>).add(<span class="hljs-number">2</span>); 
<span class="hljs-comment">// &#x6CE8;&#x610F;2&#x88AB;&#x52A0;&#x5165;&#x4E86;&#x4E24;&#x6B21;</span>

s.size <span class="hljs-comment">// 2</span>

s.has(<span class="hljs-number">1</span>) <span class="hljs-comment">// true</span>
s.has(<span class="hljs-number">2</span>) <span class="hljs-comment">// true</span>
s.has(<span class="hljs-number">3</span>) <span class="hljs-comment">// false</span>

s.delete(<span class="hljs-number">2</span>);
s.has(<span class="hljs-number">2</span>) <span class="hljs-comment">// false</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x6BD4;&#xFF0C;&#x770B;&#x770B;&#x5728;&#x5224;&#x65AD;&#x662F;&#x5426;&#x5305;&#x62EC;&#x4E00;&#x4E2A;&#x952E;&#x4E0A;&#x9762;&#xFF0C;&#x5BF9;&#x8C61;&#x7ED3;&#x6784;&#x548C;Set&#x7ED3;&#x6784;&#x7684;&#x5199;&#x6CD5;&#x4E0D;&#x540C;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// &#x5BF9;&#x8C61;&#x7684;&#x5199;&#x6CD5;</span>

<span class="hljs-keyword">var</span> properties = {
    <span class="hljs-string">&quot;width&quot;</span>: <span class="hljs-number">1</span>,
    <span class="hljs-string">&quot;height&quot;</span>: <span class="hljs-number">1</span>
};

<span class="hljs-keyword">if</span> (properties[someName]) {
    <span class="hljs-comment">// do something</span>
}

<span class="hljs-comment">// Set&#x7684;&#x5199;&#x6CD5;</span>

<span class="hljs-keyword">var</span> properties = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>();

properties.add(<span class="hljs-string">&quot;width&quot;</span>);
properties.add(<span class="hljs-string">&quot;height&quot;</span>);

<span class="hljs-keyword">if</span> (properties.has(someName)) {
    <span class="hljs-comment">// do something</span>
}
</code></pre>
<p>Array.from&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5C06;Set&#x7ED3;&#x6784;&#x8F6C;&#x4E3A;&#x6570;&#x7EC4;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> items = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]);
<span class="hljs-keyword">var</span> array = <span class="hljs-built_in">Array</span>.from(items);
</code></pre>
<p>&#x8FD9;&#x5C31;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x53BB;&#x9664;&#x6570;&#x7EC4;&#x4E2D;&#x91CD;&#x590D;&#x5143;&#x7D20;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">dedupe</span>(<span class="hljs-params">array</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>.from(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>(array));
}
</code></pre>
<h3 id="%E9%81%8D%E5%8E%86%E6%93%8D%E4%BD%9C">&#x904D;&#x5386;&#x64CD;&#x4F5C;</h3>
<p>Set&#x7ED3;&#x6784;&#x6709;&#x4E00;&#x4E2A;values&#x65B9;&#x6CD5;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-string">&apos;red&apos;</span>, <span class="hljs-string">&apos;green&apos;</span>, <span class="hljs-string">&apos;blue&apos;</span>]);
<span class="hljs-keyword">for</span> ( <span class="hljs-keyword">let</span> item <span class="hljs-keyword">of</span> set.values() ){
  <span class="hljs-built_in">console</span>.log(item);
}
<span class="hljs-comment">// red</span>
<span class="hljs-comment">// green</span>
<span class="hljs-comment">// blue</span>
</code></pre>
<p>Set&#x7ED3;&#x6784;&#x7684;&#x9ED8;&#x8BA4;&#x904D;&#x5386;&#x5668;&#x5C31;&#x662F;&#x5B83;&#x7684;values&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-built_in">Set</span>.prototype[<span class="hljs-built_in">Symbol</span>.iterator] === <span class="hljs-built_in">Set</span>.prototype.values
<span class="hljs-comment">// true</span>
</code></pre>
<p>&#x8FD9;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x53EF;&#x4EE5;&#x7701;&#x7565;values&#x65B9;&#x6CD5;&#xFF0C;&#x76F4;&#x63A5;&#x7528;for...of&#x5FAA;&#x73AF;&#x904D;&#x5386;Set&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-string">&apos;red&apos;</span>, <span class="hljs-string">&apos;green&apos;</span>, <span class="hljs-string">&apos;blue&apos;</span>]);

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> x <span class="hljs-keyword">of</span> set) {
  <span class="hljs-built_in">console</span>.log(x);
}
<span class="hljs-comment">// red</span>
<span class="hljs-comment">// green</span>
<span class="hljs-comment">// blue</span>
</code></pre>
<p>Set&#x7ED3;&#x6784;&#x7684;foreach&#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x4E8E;&#x5BF9;&#x6BCF;&#x4E2A;&#x6210;&#x5458;&#x6267;&#x884C;&#x67D0;&#x79CD;&#x64CD;&#x4F5C;&#xFF0C;&#x8FD4;&#x56DE;&#x4FEE;&#x6539;&#x540E;&#x7684;Set&#x7ED3;&#x6784;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);

set.foreach((value, key) =&gt; value*<span class="hljs-number">2</span> )
<span class="hljs-comment">// &#x8FD4;&#x56DE;Set&#x7ED3;&#x6784;{2, 4, 6}</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x8BF4;&#x660E;&#xFF0C;foreach&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5904;&#x7406;&#x51FD;&#x6570;&#x3002;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4F9D;&#x6B21;&#x4E3A;&#x952E;&#x503C;&#x3001;&#x952E;&#x540D;&#x3001;&#x96C6;&#x5408;&#x672C;&#x8EAB;&#xFF08;&#x4E0A;&#x4F8B;&#x7701;&#x7565;&#x4E86;&#x8BE5;&#x53C2;&#x6570;&#xFF09;&#x3002;&#x53E6;&#x5916;&#xFF0C;foreach&#x65B9;&#x6CD5;&#x8FD8;&#x53EF;&#x4EE5;&#x6709;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x7ED1;&#x5B9A;&#x7684;this&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x4E0E;Map&#x7ED3;&#x6784;&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#xFF0C;Set&#x7ED3;&#x6784;&#x4E5F;&#x6709;keys&#x548C;entries&#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x65F6;&#x6BCF;&#x4E2A;&#x503C;&#x7684;&#x952E;&#x540D;&#x5C31;&#x662F;&#x952E;&#x503C;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-string">&apos;red&apos;</span>, <span class="hljs-string">&apos;green&apos;</span>, <span class="hljs-string">&apos;blue&apos;</span>]);
<span class="hljs-keyword">for</span> ( <span class="hljs-keyword">let</span> item <span class="hljs-keyword">of</span> set.keys() ){
  <span class="hljs-built_in">console</span>.log(item);
}
<span class="hljs-comment">// red</span>
<span class="hljs-comment">// green</span>
<span class="hljs-comment">// blue</span>

<span class="hljs-keyword">for</span> ( <span class="hljs-keyword">let</span> [key, value] <span class="hljs-keyword">of</span> set.entries() ){
  <span class="hljs-built_in">console</span>.log(key, value);
}
<span class="hljs-comment">// red, red</span>
<span class="hljs-comment">// green, green</span>
<span class="hljs-comment">// blue, blue</span>
</code></pre>
<p>&#x7531;&#x4E8E;&#x6269;&#x5C55;&#x8FD0;&#x7B97;&#x7B26;&#xFF08;...&#xFF09;&#x5185;&#x90E8;&#x4F7F;&#x7528;for...of&#x5FAA;&#x73AF;&#xFF0C;&#x6240;&#x4EE5;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;Set&#x7ED3;&#x6784;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-string">&apos;red&apos;</span>, <span class="hljs-string">&apos;green&apos;</span>, <span class="hljs-string">&apos;blue&apos;</span>]);
<span class="hljs-keyword">let</span> arr = [...set]; 
<span class="hljs-comment">// [&apos;red&apos;, &apos;green&apos;, &apos;blue&apos;]</span>
</code></pre>
<p>&#x8FD9;&#x5C31;&#x63D0;&#x4F9B;&#x4E86;&#x53E6;&#x4E00;&#x79CD;&#x4FBF;&#x6377;&#x7684;&#x53BB;&#x9664;&#x6570;&#x7EC4;&#x91CD;&#x590D;&#x5143;&#x7D20;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> arr = [<span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>];
<span class="hljs-keyword">let</span> unique = [...new <span class="hljs-built_in">Set</span>(arr)]; 
<span class="hljs-comment">// [3, 5, 2]</span>
</code></pre>
<p>&#x800C;&#x4E14;&#xFF0C;&#x6570;&#x7EC4;&#x7684;map&#x548C;filter&#x65B9;&#x6CD5;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;Set&#x4E86;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([...set].map(x =&gt; x * <span class="hljs-number">2</span>));
<span class="hljs-comment">// &#x8FD4;&#x56DE;Set&#x7ED3;&#x6784;&#xFF1A;{2, 4, 6}</span>

<span class="hljs-keyword">let</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]);
set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([...set].filter(x =&gt; (x % <span class="hljs-number">2</span>) == <span class="hljs-number">0</span>));
<span class="hljs-comment">// &#x8FD4;&#x56DE;Set&#x7ED3;&#x6784;&#xFF1A;{2, 4}</span>
</code></pre>
<h2 id="weakset">WeakSet</h2>
<p>WeakSet&#x7ED3;&#x6784;&#x4E0E;Set&#x7C7B;&#x4F3C;&#xFF0C;&#x4E5F;&#x662F;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x503C;&#x7684;&#x96C6;&#x5408;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5B83;&#x4E0E;Set&#x6709;&#x4E24;&#x4E2A;&#x533A;&#x522B;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;WeakSet&#x7684;&#x6210;&#x5458;&#x53EA;&#x80FD;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x80FD;&#x662F;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x3002;&#x5176;&#x6B21;&#xFF0C;WeakSet&#x4E2D;&#x7684;&#x5BF9;&#x8C61;&#x90FD;&#x662F;&#x5F31;&#x5F15;&#x7528;&#xFF0C;&#x5373;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;&#x4E0D;&#x8003;&#x8651;WeakSet&#x5BF9;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5982;&#x679C;&#x5176;&#x4ED6;&#x5BF9;&#x8C61;&#x90FD;&#x4E0D;&#x518D;&#x5F15;&#x7528;&#x8BE5;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x4E48;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;&#x4F1A;&#x81EA;&#x52A8;&#x56DE;&#x6536;&#x8BE5;&#x5BF9;&#x8C61;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#xFF0C;&#x4E0D;&#x8003;&#x8651;&#x8BE5;&#x5BF9;&#x8C61;&#x8FD8;&#x5B58;&#x5728;&#x4E8E;WeakSet&#x4E4B;&#x4E2D;&#x3002;&#x8FD9;&#x4E2A;&#x7279;&#x70B9;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x65E0;&#x6CD5;&#x5F15;&#x7528;WeakSet&#x7684;&#x6210;&#x5458;&#xFF0C;&#x56E0;&#x6B64;WeakSet&#x662F;&#x4E0D;&#x53EF;&#x904D;&#x5386;&#x7684;&#x3002;</p>
<p>WeakSet&#x662F;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;new&#x547D;&#x4EE4;&#xFF0C;&#x521B;&#x5EFA;WeakSet&#x6570;&#x636E;&#x7ED3;&#x6784;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> ws = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakSet</span>();
</code></pre>
<p>&#x4F5C;&#x4E3A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;WeakSet&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x6216;&#x7C7B;&#x4F3C;&#x6570;&#x7EC4;&#x7684;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x3002;&#xFF08;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x4EFB;&#x4F55;&#x5177;&#x6709;iterable&#x63A5;&#x53E3;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;WeakSet&#x7684;&#x5BF9;&#x8C61;&#x3002;&#xFF09;&#x8BE5;&#x6570;&#x7EC4;&#x7684;&#x6240;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x90FD;&#x4F1A;&#x81EA;&#x52A8;&#x6210;&#x4E3A;WeakSet&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x7684;&#x6210;&#x5458;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> a = [[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>], [<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]];

<span class="hljs-keyword">var</span> ws = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakSet</span>(a);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;a&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x5B83;&#x6709;&#x4E24;&#x4E2A;&#x6210;&#x5458;&#xFF0C;&#x4E5F;&#x90FD;&#x662F;&#x6570;&#x7EC4;&#x3002;&#x5C06;a&#x4F5C;&#x4E3A;WeakSet&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#xFF0C;a&#x7684;&#x6210;&#x5458;&#x4F1A;&#x81EA;&#x52A8;&#x6210;&#x4E3A;WeakSet&#x7684;&#x6210;&#x5458;&#x3002;</p>
<p>WeakSet&#x7ED3;&#x6784;&#x6709;&#x4EE5;&#x4E0B;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;</p>
<ul>
<li><strong>WeakSet.prototype.add(value)</strong>&#xFF1A;&#x5411;WeakSet&#x5B9E;&#x4F8B;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x65B0;&#x6210;&#x5458;&#x3002;</li>
<li><strong>WeakSet.prototype.delete(value)</strong>&#xFF1A;&#x6E05;&#x9664;WeakSet&#x5B9E;&#x4F8B;&#x7684;&#x6307;&#x5B9A;&#x6210;&#x5458;&#x3002;</li>
<li><strong>WeakSet.prototype.has(value)</strong>&#xFF1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x67D0;&#x4E2A;&#x503C;&#x662F;&#x5426;&#x5728;WeakSet&#x5B9E;&#x4F8B;&#x4E4B;&#x4E2D;&#x3002;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> ws = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakSet</span>();
<span class="hljs-keyword">var</span> obj = {};
<span class="hljs-keyword">var</span> foo = {};

ws.add(<span class="hljs-built_in">window</span>);
ws.add(obj);

ws.has(<span class="hljs-built_in">window</span>); <span class="hljs-comment">// true</span>
ws.has(foo);    <span class="hljs-comment">// false</span>

ws.delete(<span class="hljs-built_in">window</span>); 
ws.has(<span class="hljs-built_in">window</span>);    <span class="hljs-comment">// false</span>

ws.clear();
</code></pre>
<h2 id="map">Map</h2>
<p><strong>&#xFF08;1&#xFF09;&#x57FA;&#x672C;&#x7528;&#x6CD5;</strong></p>
<p>JavaScript&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x672C;&#x8D28;&#x4E0A;&#x662F;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x4F46;&#x662F;&#x53EA;&#x80FD;&#x7528;&#x5B57;&#x7B26;&#x4E32;&#x5F53;&#x4F5C;&#x952E;&#x3002;&#x8FD9;&#x7ED9;&#x5B83;&#x7684;&#x4F7F;&#x7528;&#x5E26;&#x6765;&#x4E86;&#x5F88;&#x5927;&#x7684;&#x9650;&#x5236;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> data = {};
<span class="hljs-keyword">var</span> element = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&quot;myDiv&quot;</span>);

data[element] = metadata;
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x539F;&#x610F;&#x662F;&#x5C06;&#x4E00;&#x4E2A;DOM&#x8282;&#x70B9;&#x4F5C;&#x4E3A;&#x5BF9;&#x8C61;data&#x7684;&#x952E;&#xFF0C;&#x4F46;&#x662F;&#x7531;&#x4E8E;&#x5BF9;&#x8C61;&#x53EA;&#x63A5;&#x53D7;&#x5B57;&#x7B26;&#x4E32;&#x4F5C;&#x4E3A;&#x952E;&#x540D;&#xFF0C;&#x6240;&#x4EE5;element&#x88AB;&#x81EA;&#x52A8;&#x8F6C;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;<code>[Object HTMLDivElement]</code>&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;ES6&#x63D0;&#x4F9B;&#x4E86;map&#x6570;&#x636E;&#x7ED3;&#x6784;&#x3002;&#x5B83;&#x7C7B;&#x4F3C;&#x4E8E;&#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x662F;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x4F46;&#x662F;&#x201C;&#x952E;&#x201D;&#x7684;&#x8303;&#x56F4;&#x4E0D;&#x9650;&#x4E8E;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5404;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF08;&#x5305;&#x62EC;&#x5BF9;&#x8C61;&#xFF09;&#x90FD;&#x53EF;&#x4EE5;&#x5F53;&#x4F5C;&#x952E;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;Object&#x7ED3;&#x6784;&#x63D0;&#x4F9B;&#x4E86;&#x201C;&#x5B57;&#x7B26;&#x4E32;&#x2014;&#x503C;&#x201D;&#x7684;&#x5BF9;&#x5E94;&#xFF0C;Map&#x7ED3;&#x6784;&#x63D0;&#x4F9B;&#x4E86;&#x201C;&#x503C;&#x2014;&#x503C;&#x201D;&#x7684;&#x5BF9;&#x5E94;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> m = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();
<span class="hljs-keyword">var</span> o = {p: <span class="hljs-string">&quot;Hello World&quot;</span>};

m.set(o, <span class="hljs-string">&quot;content&quot;</span>)
m.get(o) <span class="hljs-comment">// &quot;content&quot;</span>

m.has(o) <span class="hljs-comment">// true</span>
m.delete(o) <span class="hljs-comment">// true</span>
m.has(o) <span class="hljs-comment">// false</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4F7F;&#x7528;set&#x65B9;&#x6CD5;&#xFF0C;&#x5C06;&#x5BF9;&#x8C61;o&#x5F53;&#x4F5C;m&#x7684;&#x4E00;&#x4E2A;&#x952E;&#xFF0C;&#x7136;&#x540E;&#x53C8;&#x4F7F;&#x7528;get&#x65B9;&#x6CD5;&#x8BFB;&#x53D6;&#x8FD9;&#x4E2A;&#x952E;&#xFF0C;&#x63A5;&#x7740;&#x4F7F;&#x7528;delete&#x65B9;&#x6CD5;&#x5220;&#x9664;&#x4E86;&#x8FD9;&#x4E2A;&#x952E;&#x3002;</p>
<p>&#x4F5C;&#x4E3A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;Map&#x4E5F;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x3002;&#x8BE5;&#x6570;&#x7EC4;&#x7684;&#x6210;&#x5458;&#x662F;&#x4E00;&#x4E2A;&#x4E2A;&#x8868;&#x793A;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x6570;&#x7EC4;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>([ [<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;&#x5F20;&#x4E09;&quot;</span>], [<span class="hljs-string">&quot;title&quot;</span>, <span class="hljs-string">&quot;Author&quot;</span>]]);

map.size <span class="hljs-comment">// 2</span>
map.has(<span class="hljs-string">&quot;name&quot;</span>) <span class="hljs-comment">// true</span>
map.get(<span class="hljs-string">&quot;name&quot;</span>) <span class="hljs-comment">// &quot;&#x5F20;&#x4E09;&quot;</span>
map.has(<span class="hljs-string">&quot;title&quot;</span>) <span class="hljs-comment">// true</span>
map.get(<span class="hljs-string">&quot;title&quot;</span>) <span class="hljs-comment">// &quot;Author&quot;</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x53EA;&#x6709;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#xFF0C;Map&#x7ED3;&#x6784;&#x624D;&#x5C06;&#x5176;&#x89C6;&#x4E3A;&#x540C;&#x4E00;&#x4E2A;&#x952E;&#x3002;&#x8FD9;&#x4E00;&#x70B9;&#x8981;&#x975E;&#x5E38;&#x5C0F;&#x5FC3;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();

map.set([<span class="hljs-string">&apos;a&apos;</span>], <span class="hljs-number">555</span>); 
map.get([<span class="hljs-string">&apos;a&apos;</span>]) <span class="hljs-comment">// undefined</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;set&#x548C;get&#x65B9;&#x6CD5;&#xFF0C;&#x8868;&#x9762;&#x662F;&#x9488;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x952E;&#xFF0C;&#x4F46;&#x5B9E;&#x9645;&#x4E0A;&#x8FD9;&#x662F;&#x4E24;&#x4E2A;&#x503C;&#xFF0C;&#x5185;&#x5B58;&#x5730;&#x5740;&#x662F;&#x4E0D;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x56E0;&#x6B64;get&#x65B9;&#x6CD5;&#x65E0;&#x6CD5;&#x8BFB;&#x53D6;&#x8BE5;&#x952E;&#xFF0C;&#x8FD4;&#x56DE;undefined&#x3002;</p>
<p>&#x540C;&#x7406;&#xFF0C;&#x540C;&#x6837;&#x7684;&#x503C;&#x7684;&#x4E24;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x5728;Map&#x7ED3;&#x6784;&#x4E2D;&#x88AB;&#x89C6;&#x4E3A;&#x4E24;&#x4E2A;&#x952E;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();

<span class="hljs-keyword">var</span> k1 = [<span class="hljs-string">&apos;a&apos;</span>];
<span class="hljs-keyword">var</span> k2 = [<span class="hljs-string">&apos;a&apos;</span>];

map.set(k1, <span class="hljs-number">111</span>);
map.set(k2, <span class="hljs-number">222</span>);

map.get(k1) <span class="hljs-comment">// 111</span>
map.get(k2) <span class="hljs-comment">// 222</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x53D8;&#x91CF;k1&#x548C;k2&#x7684;&#x503C;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4EEC;&#x5728;Map&#x7ED3;&#x6784;&#x4E2D;&#x88AB;&#x89C6;&#x4E3A;&#x4E24;&#x4E2A;&#x952E;&#x3002;</p>
<p>&#x7531;&#x4E0A;&#x53EF;&#x77E5;&#xFF0C;Map&#x7684;&#x952E;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x8DDF;&#x5185;&#x5B58;&#x5730;&#x5740;&#x7ED1;&#x5B9A;&#x7684;&#xFF0C;&#x53EA;&#x8981;&#x5185;&#x5B58;&#x5730;&#x5740;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;&#x5C31;&#x89C6;&#x4E3A;&#x4E24;&#x4E2A;&#x952E;&#x3002;&#x8FD9;&#x5C31;&#x89E3;&#x51B3;&#x4E86;&#x540C;&#x540D;&#x5C5E;&#x6027;&#x78B0;&#x649E;&#xFF08;clash&#xFF09;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x6211;&#x4EEC;&#x6269;&#x5C55;&#x522B;&#x4EBA;&#x7684;&#x5E93;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x952E;&#x540D;&#xFF0C;&#x5C31;&#x4E0D;&#x7528;&#x62C5;&#x5FC3;&#x81EA;&#x5DF1;&#x7684;&#x5C5E;&#x6027;&#x4E0E;&#x539F;&#x4F5C;&#x8005;&#x7684;&#x5C5E;&#x6027;&#x540C;&#x540D;&#x3002;</p>
<p>&#x5982;&#x679C;Map&#x7684;&#x952E;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF08;&#x6570;&#x5B57;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5E03;&#x5C14;&#x503C;&#xFF09;&#xFF0C;&#x5219;&#x53EA;&#x8981;&#x4E24;&#x4E2A;&#x503C;&#x4E25;&#x683C;&#x76F8;&#x7B49;&#xFF0C;Map&#x5C06;&#x5176;&#x89C6;&#x4E3A;&#x4E00;&#x4E2A;&#x952E;&#x3002;&#x8FD9;&#x5BF9;&#x4E8E;NaN&#xFF0C;&#x4EE5;&#x53CA;+0&#x548C;-0&#x90FD;&#x6210;&#x7ACB;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();

map.set(<span class="hljs-literal">NaN</span>, <span class="hljs-number">123</span>);
map.get(<span class="hljs-literal">NaN</span>) <span class="hljs-comment">// 123</span>

map.set(-<span class="hljs-number">0</span>, <span class="hljs-number">123</span>);
map.get(+<span class="hljs-number">0</span>) <span class="hljs-comment">// 123</span>
</code></pre>
<p>&#x5982;&#x679C;&#x8BFB;&#x53D6;&#x4E00;&#x4E2A;&#x672A;&#x77E5;&#x7684;&#x952E;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;undefined&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>().get(<span class="hljs-string">&apos;asfddfsasadf&apos;</span>)
<span class="hljs-comment">// undefined</span>
</code></pre>
<p><strong>&#xFF08;2&#xFF09;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</strong></p>
<p>Map&#x6570;&#x636E;&#x7ED3;&#x6784;&#x6709;&#x4EE5;&#x4E0B;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;</p>
<ul>
<li>size&#xFF1A;&#x8FD4;&#x56DE;&#x6210;&#x5458;&#x603B;&#x6570;&#x3002;</li>
<li>set(key, value)&#xFF1A;&#x8BBE;&#x7F6E;key&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x952E;&#x503C;&#xFF0C;&#x7136;&#x540E;&#x8FD4;&#x56DE;&#x6574;&#x4E2A;Map&#x7ED3;&#x6784;&#x3002;&#x5982;&#x679C;key&#x5DF2;&#x7ECF;&#x6709;&#x503C;&#xFF0C;&#x5219;&#x952E;&#x503C;&#x4F1A;&#x88AB;&#x66F4;&#x65B0;&#xFF0C;&#x5426;&#x5219;&#x5C31;&#x65B0;&#x751F;&#x6210;&#x8BE5;&#x952E;&#x3002;</li>
<li>get(key)&#xFF1A;&#x8BFB;&#x53D6;key&#x5BF9;&#x5E94;&#x7684;&#x952E;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x627E;&#x4E0D;&#x5230;key&#xFF0C;&#x8FD4;&#x56DE;undefined&#x3002;</li>
<li>has(key)&#xFF1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x67D0;&#x4E2A;&#x952E;&#x662F;&#x5426;&#x5728;Map&#x6570;&#x636E;&#x7ED3;&#x6784;&#x4E2D;&#x3002;</li>
<li>delete(key)&#xFF1A;&#x5220;&#x9664;&#x67D0;&#x4E2A;&#x952E;&#xFF0C;&#x8FD4;&#x56DE;true&#x3002;&#x5982;&#x679C;&#x5220;&#x9664;&#x5931;&#x8D25;&#xFF0C;&#x8FD4;&#x56DE;false&#x3002;</li>
<li>clear()&#xFF1A;&#x6E05;&#x9664;&#x6240;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x3002;</li>
</ul>
<p>set()&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;&#x662F;Map&#x672C;&#x8EAB;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x91C7;&#x7528;&#x94FE;&#x5F0F;&#x5199;&#x6CD5;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>()
  .set(<span class="hljs-number">1</span>, <span class="hljs-string">&apos;a&apos;</span>)
  .set(<span class="hljs-number">2</span>, <span class="hljs-string">&apos;b&apos;</span>)
  .set(<span class="hljs-number">3</span>, <span class="hljs-string">&apos;c&apos;</span>);
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;has()&#x548C;delete()&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> m = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>(); 

m.set(<span class="hljs-string">&quot;edition&quot;</span>, <span class="hljs-number">6</span>)        <span class="hljs-comment">// &#x952E;&#x662F;&#x5B57;&#x7B26;&#x4E32;</span>
m.set(<span class="hljs-number">262</span>, <span class="hljs-string">&quot;standard&quot;</span>)     <span class="hljs-comment">// &#x952E;&#x662F;&#x6570;&#x503C;</span>
m.set(<span class="hljs-literal">undefined</span>, <span class="hljs-string">&quot;nah&quot;</span>)    <span class="hljs-comment">// &#x952E;&#x662F;undefined</span>

<span class="hljs-keyword">var</span> hello = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;hello&quot;</span>);}
m.set(hello, <span class="hljs-string">&quot;Hello ES6!&quot;</span>) <span class="hljs-comment">// &#x952E;&#x662F;&#x51FD;&#x6570;</span>

m.has(<span class="hljs-string">&quot;edition&quot;</span>)     <span class="hljs-comment">// true</span>
m.has(<span class="hljs-string">&quot;years&quot;</span>)       <span class="hljs-comment">// false</span>
m.has(<span class="hljs-number">262</span>)           <span class="hljs-comment">// true</span>
m.has(<span class="hljs-literal">undefined</span>)     <span class="hljs-comment">// true</span>
m.has(hello)         <span class="hljs-comment">// true</span>

m.delete(<span class="hljs-literal">undefined</span>)
m.has(<span class="hljs-literal">undefined</span>)       <span class="hljs-comment">// false</span>

m.get(hello)  <span class="hljs-comment">// Hello ES6!</span>
m.get(<span class="hljs-string">&quot;edition&quot;</span>)  <span class="hljs-comment">// 6</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;size&#x5C5E;&#x6027;&#x548C;clear&#x65B9;&#x6CD5;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();
map.set(<span class="hljs-string">&apos;foo&apos;</span>, <span class="hljs-literal">true</span>);
map.set(<span class="hljs-string">&apos;bar&apos;</span>, <span class="hljs-literal">false</span>);

map.size <span class="hljs-comment">// 2</span>
map.clear()
map.size <span class="hljs-comment">// 0</span>
</code></pre>
<p><strong>&#xFF08;3&#xFF09;&#x904D;&#x5386;</strong></p>
<p>Map&#x539F;&#x751F;&#x63D0;&#x4F9B;&#x4E09;&#x4E2A;&#x904D;&#x5386;&#x5668;&#x3002;</p>
<ul>
<li>keys()&#xFF1A;&#x8FD4;&#x56DE;&#x952E;&#x540D;&#x7684;&#x904D;&#x5386;&#x5668;&#x3002;</li>
<li>values()&#xFF1A;&#x8FD4;&#x56DE;&#x952E;&#x503C;&#x7684;&#x904D;&#x5386;&#x5668;&#x3002;</li>
<li>entries()&#xFF1A;&#x8FD4;&#x56DE;&#x6240;&#x6709;&#x6210;&#x5458;&#x7684;&#x904D;&#x5386;&#x5668;&#x3002;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x662F;&#x4F7F;&#x7528;&#x5B9E;&#x4F8B;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>([
  [<span class="hljs-string">&apos;F&apos;</span>, <span class="hljs-string">&apos;no&apos;</span>],
  [<span class="hljs-string">&apos;T&apos;</span>,  <span class="hljs-string">&apos;yes&apos;</span>],
]);

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> key <span class="hljs-keyword">of</span> map.keys()) {
  <span class="hljs-built_in">console</span>.log(key);
}
<span class="hljs-comment">// &quot;F&quot;</span>
<span class="hljs-comment">// &quot;T&quot;</span>

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> value <span class="hljs-keyword">of</span> map.values()) {
  <span class="hljs-built_in">console</span>.log(value);
}
<span class="hljs-comment">// &quot;no&quot;</span>
<span class="hljs-comment">// &quot;yes&quot;</span>

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> item <span class="hljs-keyword">of</span> map.entries()) {
  <span class="hljs-built_in">console</span>.log(item[<span class="hljs-number">0</span>], item[<span class="hljs-number">1</span>]);
}
<span class="hljs-comment">// &quot;F&quot; &quot;no&quot;</span>
<span class="hljs-comment">// &quot;T&quot; &quot;yes&quot;</span>

<span class="hljs-comment">// &#x6216;&#x8005;</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> [key, value] <span class="hljs-keyword">of</span> map.entries()) {
  <span class="hljs-built_in">console</span>.log(key, value);
}

<span class="hljs-comment">// &#x7B49;&#x540C;&#x4E8E;&#x4F7F;&#x7528;map.entries()</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> [key, value] <span class="hljs-keyword">of</span> map) {
  <span class="hljs-built_in">console</span>.log(key, value);
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x6700;&#x540E;&#x7684;&#x90A3;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x8868;&#x793A;Map&#x7ED3;&#x6784;&#x7684;&#x9ED8;&#x8BA4;&#x904D;&#x5386;&#x5668;&#x63A5;&#x53E3;&#xFF08;Symbol.iterator&#x5C5E;&#x6027;&#xFF09;&#xFF0C;&#x5C31;&#x662F;entries&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-javascript">
map[<span class="hljs-built_in">Symbol</span>.iterator] === map.entries
<span class="hljs-comment">// true</span>
</code></pre>
<p>Map&#x7ED3;&#x6784;&#x8F6C;&#x4E3A;&#x6570;&#x7EC4;&#x7ED3;&#x6784;&#xFF0C;&#x6BD4;&#x8F83;&#x5FEB;&#x901F;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x7ED3;&#x5408;&#x4F7F;&#x7528;&#x6269;&#x5C55;&#x8FD0;&#x7B97;&#x7B26;&#xFF08;...&#xFF09;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>([
  [<span class="hljs-number">1</span>, <span class="hljs-string">&apos;one&apos;</span>],
  [<span class="hljs-number">2</span>, <span class="hljs-string">&apos;two&apos;</span>],
  [<span class="hljs-number">3</span>, <span class="hljs-string">&apos;three&apos;</span>],
]);

[...map.keys()]
<span class="hljs-comment">// [1, 2, 3]</span>

[...map.values()]
<span class="hljs-comment">// [&apos;one&apos;, &apos;two&apos;, &apos;three&apos;]</span>

[...map.entries()]
<span class="hljs-comment">// [[1,&apos;one&apos;], [2, &apos;two&apos;], [3, &apos;three&apos;]]</span>

[...map]
<span class="hljs-comment">// [[1,&apos;one&apos;], [2, &apos;two&apos;], [3, &apos;three&apos;]]</span>
</code></pre>
<p>&#x7ED3;&#x5408;&#x6570;&#x7EC4;&#x7684;map&#x65B9;&#x6CD5;&#x3001;filter&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;Map&#x7684;&#x904D;&#x5386;&#x548C;&#x8FC7;&#x6EE4;&#xFF08;Map&#x672C;&#x8EAB;&#x6CA1;&#x6709;map&#x548C;filter&#x65B9;&#x6CD5;&#xFF09;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> map0 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>()
  .set(<span class="hljs-number">1</span>, <span class="hljs-string">&apos;a&apos;</span>)
  .set(<span class="hljs-number">2</span>, <span class="hljs-string">&apos;b&apos;</span>)
  .set(<span class="hljs-number">3</span>, <span class="hljs-string">&apos;c&apos;</span>);

<span class="hljs-keyword">let</span> map1 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>(
  [...map0].filter(([k, v]) =&gt; k &lt; <span class="hljs-number">3</span>) 
);
<span class="hljs-comment">// &#x4EA7;&#x751F;Map&#x7ED3;&#x6784; {1 =&gt; &apos;a&apos;, 2 =&gt; &apos;b&apos;}</span>

<span class="hljs-keyword">let</span> map2 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>(
  [...map0].map(([k, v]) =&gt; [k * <span class="hljs-number">2</span>, <span class="hljs-string">&apos;_&apos;</span> + v])
    );
<span class="hljs-comment">// &#x4EA7;&#x751F;Map&#x7ED3;&#x6784; {2 =&gt; &apos;_a&apos;, 4 =&gt; &apos;_b&apos;, 6 =&gt; &apos;_c&apos;}</span>
</code></pre>
<p>&#x6B64;&#x5916;&#xFF0C;Map&#x8FD8;&#x6709;&#x4E00;&#x4E2A;forEach&#x65B9;&#x6CD5;&#xFF0C;&#x4E0E;&#x6570;&#x7EC4;&#x7684;forEach&#x65B9;&#x6CD5;&#x7C7B;&#x4F3C;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x904D;&#x5386;&#x3002;</p>
<pre><code class="lang-javascript">
map.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">value, key, map</span>)) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Key: %s, Value: %s&quot;</span>, key, value);
};
</code></pre>
<p>forEach&#x65B9;&#x6CD5;&#x8FD8;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x7ED1;&#x5B9A;this&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> reporter = {
  report: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">key, value</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Key: %s, Value: %s&quot;</span>, key, value);
  }
};

map.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">value, key, map</span>) </span>{
  <span class="hljs-keyword">this</span>.report(key, value);
}, reporter);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;forEach&#x65B9;&#x6CD5;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;this&#xFF0C;&#x5C31;&#x6307;&#x5411;reporter&#x3002;</p>
<h2 id="weakmap">WeakMap</h2>
<p>WeakMap&#x7ED3;&#x6784;&#x4E0E;Map&#x7ED3;&#x6784;&#x57FA;&#x672C;&#x7C7B;&#x4F3C;&#xFF0C;&#x552F;&#x4E00;&#x7684;&#x533A;&#x522B;&#x662F;&#x5B83;&#x53EA;&#x63A5;&#x53D7;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x952E;&#x540D;&#xFF08;null&#x9664;&#x5916;&#xFF09;&#xFF0C;&#x4E0D;&#x63A5;&#x53D7;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x4F5C;&#x4E3A;&#x952E;&#x540D;&#xFF0C;&#x800C;&#x4E14;&#x952E;&#x540D;&#x6240;&#x6307;&#x5411;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0D;&#x8BA1;&#x5165;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;&#x3002;</p>
<p>WeakMap&#x7684;&#x8BBE;&#x8BA1;&#x76EE;&#x7684;&#x5728;&#x4E8E;&#xFF0C;&#x952E;&#x540D;&#x662F;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#xFF08;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;&#x4E0D;&#x5C06;&#x8BE5;&#x5F15;&#x7528;&#x8003;&#x8651;&#x5728;&#x5185;&#xFF09;&#xFF0C;&#x6240;&#x4EE5;&#x5176;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x5BF9;&#x8C61;&#x53EF;&#x80FD;&#x4F1A;&#x88AB;&#x81EA;&#x52A8;&#x56DE;&#x6536;&#x3002;&#x5F53;&#x5BF9;&#x8C61;&#x88AB;&#x56DE;&#x6536;&#x540E;&#xFF0C;WeakMap&#x81EA;&#x52A8;&#x79FB;&#x9664;&#x5BF9;&#x5E94;&#x7684;&#x952E;&#x503C;&#x5BF9;&#x3002;&#x5178;&#x578B;&#x5E94;&#x7528;&#x662F;&#xFF0C;&#x4E00;&#x4E2A;&#x5BF9;&#x5E94;DOM&#x5143;&#x7D20;&#x7684;WeakMap&#x7ED3;&#x6784;&#xFF0C;&#x5F53;&#x67D0;&#x4E2A;DOM&#x5143;&#x7D20;&#x88AB;&#x6E05;&#x9664;&#xFF0C;&#x5176;&#x6240;&#x5BF9;&#x5E94;&#x7684;WeakMap&#x8BB0;&#x5F55;&#x5C31;&#x4F1A;&#x81EA;&#x52A8;&#x88AB;&#x79FB;&#x9664;&#x3002;&#x57FA;&#x672C;&#x4E0A;&#xFF0C;WeakMap&#x7684;&#x4E13;&#x7528;&#x573A;&#x5408;&#x5C31;&#x662F;&#xFF0C;&#x5B83;&#x7684;&#x952E;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x5728;&#x5C06;&#x6765;&#x6D88;&#x5931;&#x3002;WeakMap&#x7ED3;&#x6784;&#x6709;&#x52A9;&#x4E8E;&#x9632;&#x6B62;&#x5185;&#x5B58;&#x6CC4;&#x6F0F;&#x3002; </p>
<p>&#x4E0B;&#x9762;&#x662F;WeakMap&#x7ED3;&#x6784;&#x7684;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x7528;&#x6CD5;&#x4E0A;&#x4E0E;Map&#x51E0;&#x4E4E;&#x4E00;&#x6837;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakMap</span>();
<span class="hljs-keyword">var</span> element = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&quot;.element&quot;</span>);

map.set(element, <span class="hljs-string">&quot;Original&quot;</span>);

<span class="hljs-keyword">var</span> value = map.get(element);
<span class="hljs-built_in">console</span>.log(value); <span class="hljs-comment">// &quot;Original&quot;</span>

element.parentNode.removeChild(element);
element = <span class="hljs-literal">null</span>;

value = map.get(element);
<span class="hljs-built_in">console</span>.log(value); <span class="hljs-comment">// undefined</span>
</code></pre>
<p>WeakMap&#x4E0E;Map&#x5728;API&#x4E0A;&#x7684;&#x533A;&#x522B;&#x4E3B;&#x8981;&#x662F;&#x4E24;&#x4E2A;&#xFF0C;&#x4E00;&#x662F;&#x6CA1;&#x6709;&#x904D;&#x5386;&#x64CD;&#x4F5C;&#xFF08;&#x5373;&#x6CA1;&#x6709;key()&#x3001;values()&#x548C;entries()&#x65B9;&#x6CD5;&#xFF09;&#xFF0C;&#x4E5F;&#x6CA1;&#x6709;size&#x5C5E;&#x6027;&#xFF1B;&#x4E8C;&#x662F;&#x65E0;&#x6CD5;&#x6E05;&#x7A7A;&#xFF0C;&#x5373;&#x4E0D;&#x652F;&#x6301;clear&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E0E;WeakMap&#x7684;&#x952E;&#x4E0D;&#x88AB;&#x8BA1;&#x5165;&#x5F15;&#x7528;&#x3001;&#x88AB;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;&#x5FFD;&#x7565;&#x6709;&#x5173;&#x3002;&#x56E0;&#x6B64;&#xFF0C;WeakMap&#x53EA;&#x6709;&#x56DB;&#x4E2A;&#x65B9;&#x6CD5;&#x53EF;&#x7528;&#xFF1A;get()&#x3001;set()&#x3001;has()&#x3001;delete()&#x3002;</p>
<p>WeakMap&#x7684;&#x4E00;&#x4E2A;&#x7528;&#x5904;&#x662F;&#x90E8;&#x7F72;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> _counter = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakMap</span>();
<span class="hljs-keyword">let</span> _action = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakMap</span>();

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Countdown</span> </span>{
  constructor(counter, action) {
    _counter.set(<span class="hljs-keyword">this</span>, counter);
    _action.set(<span class="hljs-keyword">this</span>, action);
  }
  dec() {
    <span class="hljs-keyword">let</span> counter = _counter.get(<span class="hljs-keyword">this</span>);
    <span class="hljs-keyword">if</span> (counter &lt; <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span>;
    counter--;
    _counter.set(<span class="hljs-keyword">this</span>, counter);
    <span class="hljs-keyword">if</span> (counter === <span class="hljs-number">0</span>) {
      _action.get(<span class="hljs-keyword">this</span>)();
    }
  }
}

<span class="hljs-keyword">let</span> c = <span class="hljs-keyword">new</span> Countdown(<span class="hljs-number">2</span>, () =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;DONE&apos;</span>));

c.dec()
c.dec()
<span class="hljs-comment">// DONE</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;Countdown&#x7C7B;&#x7684;&#x4E24;&#x4E2A;&#x5185;&#x90E8;&#x5C5E;&#x6027;_counter&#x548C;_action&#xFF0C;&#x662F;&#x5B9E;&#x4F8B;&#x7684;&#x5F31;&#x5F15;&#x7528;&#xFF0C;&#x6240;&#x4EE5;&#x5982;&#x679C;&#x5220;&#x9664;&#x5B9E;&#x4F8B;&#xFF0C;&#x5B83;&#x4EEC;&#x4E5F;&#x5C31;&#x968F;&#x4E4B;&#x6D88;&#x5931;&#xFF0C;&#x4E0D;&#x4F1A;&#x9020;&#x6210;&#x5185;&#x5B58;&#x6CC4;&#x6F0F;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="function.html" class="navigation navigation-prev " aria-label="Previous page: &#x51FD;&#x6570;&#x7684;&#x6269;&#x5C55;"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="iterator.html" class="navigation navigation-next " aria-label="Next page: Iterator&#x548C;for...of&#x5FAA;&#x73AF;"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="static/js/app.js"></script>

    
    <script src="static/js/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"disqus":{"shortName":"git-book"}};
    gitbook.start(config);
});
</script>

        
    
    

</body></html>